using System;
using UmengAnalyticsNet.Extension;

namespace UmengSDK.Common
{
    internal class UmengSettings
    {
        private const string SETTINGS_NAME = "UmengSettings.xml";

        private static SerializableDictionary<string, object> _settingsDic = null;

        private static readonly object syncObj = new object();

        public static void Load()
        {
            try
            {
                lock (syncObj)
                {
                    var dic = SettingEx.Load<SerializableDictionary<string, object>>(SETTINGS_NAME);
                    if (dic == null)
                    {
                        dic = new SerializableDictionary<string, object>();
                    }

                    _settingsDic = dic;
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log(e);
            }
        }

        public static void Save()
        {
            try
            {
                lock (syncObj)
                {
                    var dic = _settingsDic;
                    if (dic != null && dic.Count > 0)
                    {
                        _settingsDic.Save(SETTINGS_NAME);
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log(e);
            }
        }

        public static bool Contains(string key)
        {
            try
            {
                lock (syncObj)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        return _settingsDic.ContainsKey(key);
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in put UmengSettings", e);
            }
            return false;
        }

        public static void Put(string key, object value)
        {
            try
            {
                lock (syncObj)
                {
                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        _settingsDic[key] = value;
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in put UmengSettings", e);
            }
        }

        public static void Clear(string key)
        {
            try
            {
                lock (syncObj)
                {
                    _settingsDic.Remove(key);
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in clear UmengSettings", e);
            }
        }

        public static T Get<T>(string key, T defaultValue)
        {
            try
            {
                if (string.IsNullOrEmpty(key))
                {
                    T result = defaultValue;
                    return result;
                }
                if (_settingsDic.ContainsKey(key))
                {
                    T result = (T)(_settingsDic[key]);
                    return result;
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in get<T> UmengSettings", e);
            }
            return default(T);
        }

        public static void Acc(string key, int delta)
        {
            try
            {
                lock (syncObj)
                {
                    if (_settingsDic.ContainsKey(key))
                    {
                        int num = (int)_settingsDic[key];
                        _settingsDic[key] = num + delta;
                    }
                    else
                    {
                        _settingsDic.Add(key, delta);
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in getInt UmengSettings", e);
            }
        }

        public static void Acc(string key, string msg)
        {
            try
            {
                lock (syncObj)
                {
                    if (_settingsDic.ContainsKey(key))
                    {
                        string text = (string)_settingsDic[key];
                        _settingsDic[key] = text + ";" + msg;
                    }
                    else
                    {
                        _settingsDic.Add(key, msg);
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log("error in getInt UmengSettings", e);
            }
        }
    }
}
